import java.util.*;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static java.lang.Thread.sleep;

public class ThreadTest {

    public static void main(String[] args) throws InterruptedException {
//        Thread t = new Thread(() -> {
//            System.out.println("hello nihao");
//            System.out.println("hello");
//        });
//        System.out.println("start");
//        t.setDaemon(true);
//        t.start();

//        t.join();  //该线程执行完以后才执行后续的线程。
//        System.out.println("end");


        StringBuilder stringBuilder = new StringBuilder();

        String cronExpression = "5 * * ?";
        int ran = (int) (Math.random() * (59 - 0 + 1));


        stringBuilder.append(ran + " ");

        ran = (int) (Math.random() * (59 - 0 + 1));
        stringBuilder.append(ran + " ");

        stringBuilder.append(cronExpression);

        System.out.println(stringBuilder.toString());

        List<TMemberGroupPortraitTagData> resultList = new ArrayList<>();
        TMemberGroupPortraitTagData tMemberGroupPortraitTagData = new TMemberGroupPortraitTagData();
        tMemberGroupPortraitTagData.setChannelCode("1");
        tMemberGroupPortraitTagData.setTagCode("bb_age_stage");
        tMemberGroupPortraitTagData.setEnumValue("1");
        tMemberGroupPortraitTagData.setEnumName("未知");
        tMemberGroupPortraitTagData.setMemberNum(10L);

        resultList.add(tMemberGroupPortraitTagData);



        TMemberGroupPortraitTagData tMemberGroupPortraitTagData1 = new TMemberGroupPortraitTagData();
        tMemberGroupPortraitTagData1.setChannelCode("1");
        tMemberGroupPortraitTagData1.setTagCode("bb_age_stage");
        tMemberGroupPortraitTagData1.setEnumValue("2");
        tMemberGroupPortraitTagData1.setEnumName("未知");
        tMemberGroupPortraitTagData1.setMemberNum(30L);


        resultList.add(tMemberGroupPortraitTagData1);


        TMemberGroupPortraitTagData tMemberGroupPortraitTagData2 = new TMemberGroupPortraitTagData();
        tMemberGroupPortraitTagData2.setChannelCode("1");
        tMemberGroupPortraitTagData2.setTagCode("bb_age_stage");
        tMemberGroupPortraitTagData2.setEnumValue("2");
        tMemberGroupPortraitTagData2.setEnumName("婴儿");
        tMemberGroupPortraitTagData2.setMemberNum(40L);


        resultList.add(tMemberGroupPortraitTagData2);
        processUnknown(resultList);
        System.out.println(resultList);

    }


    public static void processUnknown(List<TMemberGroupPortraitTagData> tMemberGroupPortraitDataList) {


        Map<String, List<TMemberGroupPortraitTagData>> channelMap = tMemberGroupPortraitDataList.stream().collect(Collectors.groupingBy(TMemberGroupPortraitTagData::getChannelCode));
        if (channelMap != null) {
            //渠道下的画像列表
            Iterator<Map.Entry<String, List<TMemberGroupPortraitTagData>>> channelMapIterator = channelMap.entrySet().iterator();
            while (channelMapIterator.hasNext()) {
                Map.Entry<String, List<TMemberGroupPortraitTagData>> next = channelMapIterator.next();
                List<TMemberGroupPortraitTagData> channelPortraitData = next.getValue();

                String channelCode = next.getKey();
                //标签key为空的需要过滤掉
                List<TMemberGroupPortraitTagData> notNullchannelPortraitData = channelPortraitData.stream().filter(x -> Objects.nonNull(x.getTagCode())).collect(Collectors.toList());
                //渠道下每个标签对应的画像列表
                Map<String, List<TMemberGroupPortraitTagData>> tagCodeMap = notNullchannelPortraitData.stream().collect(Collectors.groupingBy(TMemberGroupPortraitTagData::getTagCode));

                if (tagCodeMap != null) {
                    Iterator<Map.Entry<String, List<TMemberGroupPortraitTagData>>> tagCodePortraitIterator = tagCodeMap.entrySet().iterator();
                    while (tagCodePortraitIterator.hasNext()) {
                        Map.Entry<String, List<TMemberGroupPortraitTagData>> nextPortrait = tagCodePortraitIterator.next();
                        String tagCode = nextPortrait.getKey();
                        List<TMemberGroupPortraitTagData> tagCodePortraitDatas = nextPortrait.getValue();
                        Map<String, List<TMemberGroupPortraitTagData>> tagEnumNameMap = tagCodePortraitDatas.stream().filter(x -> Objects.nonNull(x.getEnumName())).collect(Collectors.groupingBy(TMemberGroupPortraitTagData::getEnumName));
                        List<TMemberGroupPortraitTagData> unknownList = tagEnumNameMap.get("未知");
                        //先将都为未知的数据的数据
                        if (unknownList!=null) {
                            Long sum = unknownList.stream().map(x -> x.getMemberNum()).reduce((x, y) -> x += y).get();

                            TMemberGroupPortraitTagData tMemberGroupPortraitTagDataOld = tagCodePortraitDatas.get(0);
                            TMemberGroupPortraitTagData tMemberGroupPortraitTagDataNew = new TMemberGroupPortraitTagData();

                            tMemberGroupPortraitTagDataNew.setMemberGroupId(tMemberGroupPortraitTagDataOld.getMemberGroupId());
                            tMemberGroupPortraitTagDataNew.setChannelCode(channelCode);
                            tMemberGroupPortraitTagDataNew.setTagCode(tagCode);
                            tMemberGroupPortraitTagDataNew.setEnumValue("-1");
                            tMemberGroupPortraitTagDataNew.setEnumName("未知");
                            tMemberGroupPortraitTagDataNew.setMemberNum(sum);
                            tMemberGroupPortraitTagDataNew.setDataType(1);
                            tMemberGroupPortraitTagDataNew.setStatus(1);
                            tMemberGroupPortraitTagDataNew.setDeleteFlag(0);
                            tMemberGroupPortraitDataList.add(tMemberGroupPortraitTagDataNew);

                            //将数据移除
                            tMemberGroupPortraitDataList.removeAll(unknownList);
                        }
                    }
                }
            }
        }

    }


}
